package com.gopay.scheduler.job;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.gopay.common.cps.dao.autosettle.AsBankLimitConfigDAO;
import com.gopay.common.cps.service.autosettle.impl.AutoSettleHelper;
import com.gopay.common.domain.autosettle.AsBankLimitConfig;
import com.gopay.common.domain.bank.route.po.pay.PayNuccChannel;
import com.gopay.common.domain.bank.route.po.pay.PayNuccOptLog;
import com.gopay.common.domain.bank.route.po.pay.PayNuccRules;
import com.gopay.common.util.DateUtils;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.nucc.AutoSwitchAcctForNuccService;

/**
 * 网联自动切换出款账户Job
 *
 * @author lynn
 * @data
 */
public class AutoSwitchAcctForNuccJob extends GopayBaseJob {

	// 日志
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "autoSwitchAcctForNuccService")
	private AutoSwitchAcctForNuccService autoSwitchAcctForNuccService;

	@Resource(name = "asBankLimitConfigDAO")
	private AsBankLimitConfigDAO asBankLimitConfigDAO;

	@Resource(name = "autoSettleHelper")
	private AutoSettleHelper autoSettleHelper;

	@Override
	protected void doJobInternal() {
		logger.info("-------------AutoSwitchAcctForNuccJob start---------------");

		try {
			// 当前时间
			Date curDate = new Date();
			String sysDate = DateUtils.getCurrDate("HH:mm:ss");
			String whFlag = "0"; // 0.
			if (autoSettleHelper.isHoliday(curDate)) {
				whFlag = "2";
			} else {
				whFlag = "1";
			}

			// 获取当前可操作网联出款通道ID
			String type = "1"; // 0: 不可操作 1：可操作
			logger.info("-----------AutoSwitchAcctForNuccJob type-----------"+type+"--whFlag--"+whFlag);
			List<PayNuccChannel> payNuccChannels = autoSwitchAcctForNuccService.findPayNuccChannelByType(type);
			logger.info("-----------AutoSwitchAcctForNuccJob payNuccChannels-----------"+payNuccChannels);
			logger.info("-----------AutoSwitchAcctForNuccJob payNuccChannels size-----------"+payNuccChannels.size());
			if (null != payNuccChannels && payNuccChannels.size() > 0) {
				// 循环处理银企付款批量订单
				for (int i = 0; i < payNuccChannels.size(); i++) {
					PayNuccChannel payNuccChannel = payNuccChannels.get(i);
					long channelId = payNuccChannel.getId().longValue();
					logger.info("-----------AutoSwitchAcctForNuccJob channelId-----------"+channelId);
					AsBankLimitConfig asBankLimitConfig = asBankLimitConfigDAO.findByBankCode(payNuccChannel.getRecBankCode());
					List<PayNuccRules> payNuccRules = autoSwitchAcctForNuccService.findPayNuccRules(channelId, whFlag, sysDate, asBankLimitConfig.getBankAcctAmt());
					logger.info("-----------AutoSwitchAcctForNuccJob payNuccRules size-----------"+payNuccRules.size());
					if (null != payNuccRules && payNuccRules.size() > 0) {
						if (payNuccChannel.getDefBankCode().equals(payNuccChannel.getCurBankCode())) {
							String payBankCode = payNuccRules.get(0).getPayBankCode();
							logger.info("-----------AutoSwitchAcctForNuccJob payBankCode-----------"+payBankCode);
							PayNuccChannel icbcPayNuccChannel = autoSwitchAcctForNuccService.findPayNuccChannelByRecBankCode(payBankCode); // 获取工行备付金账户信息
							//组装保存日志对象
							PayNuccOptLog optLog = new PayNuccOptLog();
							optLog.setCreatedTs(curDate);
							optLog.setCurBankCode(payBankCode);
							optLog.setPreBankCode(payNuccChannel.getCurBankCode());
							optLog.setRecBankCode(payNuccChannel.getRecBankCode());
							optLog.setOptId("System");
							optLog.setType("1"); // 0、手动切换 1、自动切换
							
							payNuccChannel.setCurBankCode(payBankCode);
							payNuccChannel.setCurBankName(icbcPayNuccChannel.getRecBankName());
							payNuccChannel.setCurBankAcct(icbcPayNuccChannel.getDefBankAcct());
							payNuccChannel.setCurBankAcctName(icbcPayNuccChannel.getDefBankAcctName());
							autoSwitchAcctForNuccService.updatePayNuccChannel(payNuccChannel,optLog);
							sendAutoSwitchSMS(payNuccChannel,payNuccRules.get(0));
						}
					} else {
						// 判断是否有勾选不满足条件时切回默认账户
						String rollBackFlag = "1"; // 0、不可恢复 1、可恢复
						PayNuccRules payNuccRule = autoSwitchAcctForNuccService.findPayNuccRulesByChannIdAndRollFlag(channelId, rollBackFlag);
						logger.info("-----------AutoSwitchAcctForNuccJob payNuccRule-----------"+ToStringBuilder.reflectionToString(payNuccRule));
						if (null != payNuccRule && !payNuccChannel.getDefBankCode().equals(payNuccChannel.getCurBankCode())) {
							//组装保存日志对象
							PayNuccOptLog optLog = new PayNuccOptLog();
							optLog.setCreatedTs(curDate);
							optLog.setCurBankCode(payNuccChannel.getDefBankCode());
							optLog.setPreBankCode(payNuccChannel.getCurBankCode());
							optLog.setRecBankCode(payNuccChannel.getRecBankCode());
							optLog.setOptId("System");
							optLog.setType("1"); // 0、手动切换 1、自动切换
							
							payNuccChannel.setCurBankCode(payNuccChannel.getDefBankCode());
							payNuccChannel.setCurBankName(payNuccChannel.getDefBankName());
							payNuccChannel.setCurBankAcct(payNuccChannel.getDefBankAcct());
							payNuccChannel.setCurBankAcctName(payNuccChannel.getDefBankAcctName());
							autoSwitchAcctForNuccService.updatePayNuccChannel(payNuccChannel,optLog);
							sendAutoSwitchSMS(payNuccChannel,payNuccRule);

						}
					}
				}

			}
		} catch (Exception e) {
			logger.info("-------------AutoSwitchAcctForNuccJob error---------------");
			logger.error(e.getMessage(),e);
		} finally {
			logger.info("-------------AutoSwitchAcctForNuccJob end---------------");
		}
	}

	 private void  sendAutoSwitchSMS(PayNuccChannel payNuccChannel,PayNuccRules payNuccRule){
		 logger.info("---------AutoSwitchAcctForNuccJob 发送短信开始----------");
		 try{
			 autoSwitchAcctForNuccService.sendAutoSwitchSMS(payNuccChannel, payNuccRule);
		 }catch(Exception e){
			 logger.info("---------AutoSwitchAcctForNuccJob 发送短信错误----------");
			 logger.error(e.getMessage(),e);
		 }
		 logger.info("---------AutoSwitchAcctForNuccJob 发送短信结束----------");
	 }

}
